package com.example.treehole;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.treehole.R;
import com.example.treehole.bean.Course;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;

import java.util.HashMap;

public class AddCourseActivity extends AppCompatActivity {
    private EditText etCourseName, etClassroom, etClassTime;
    private int dayOfWeek;
    private int classNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_course);

        etCourseName = findViewById(R.id.et_course_name);
        etClassroom = findViewById(R.id.et_classroom);
        etClassTime = findViewById(R.id.et_class_time);

        findViewById(R.id.backBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveCourse();
            }
        });
        classNumber = getIntent().getIntExtra("classNumber", 0);
        dayOfWeek = getIntent().getIntExtra("dayOfWeek", 0);
    }

    private void saveCourse() {
        String courseName = etCourseName.getText().toString().trim()+"\n";
        String classroom = etClassroom.getText().toString().trim()+"\n";
        String classTime = etClassTime.getText().toString().trim();

        if (courseName.isEmpty() || classroom.isEmpty() || classTime.isEmpty()) {
            Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
            return;
        }
        Course course = new Course();
        course.setName(courseName);
        course.setLocation(classroom);
        course.setTime(classTime);
        course.setDayOfWeek(dayOfWeek);
        course.setClassNumber(classNumber);
        course.setUserid(App.user.id);
        HttpUtil.post(Api.COURSE_ADD, HttpUtil.toJSON(course), new HttpUtil.OnNetStringListener() {
            @Override
            public void success(String data, String message) {
                Toast.makeText(AddCourseActivity.this, "课程保存成功", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void error(String message) {

            }
        });
    }
}